#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_COST 9
#define MAX_CARD (treasure_map+1)
#define NUM_SUPPLY 10

void testBuyCard (void)
{
    struct gameState G;
    int k[NUM_SUPPLY] =
    {
        adventurer, gardens, embargo, village, minion, mine, cutpurse,
        sea_hag, tribute, smithy
    };
    
    initializeGame(2, k, 1, &G);
    
    G.numBuys = 0;
    if (buyCard (copper, &G) == -1)
    {
        printf ("%s: PASS when attempting to buy copper without any buys.\n", __func__);
    }
    else
    {
        printf ("%s: FAIL when attempting to buy copper without any buys.\n", __func__);
        return;
    }
    
    for (int moneyamt = 0; moneyamt < MAX_COST; moneyamt++)
    {
        for (int trybuy = 0; trybuy < MAX_CARD; trybuy++)
        {
            G.coins = moneyamt;
            G.numBuys = 1;
            
            bool buyFailed = buyCard (trybuy, &G) == -1;
            
            bool expectFail_unavailable = false;
            if (trybuy > gold)
            {
                int i;
                for (i = 0; i < NUM_SUPPLY; i++)
                {
                    if (k[i] == trybuy)
                        break;
                }
                
                if (i == NUM_SUPPLY)
                    expectFail_unavailable = true;
            }
            
            bool expectFail_tooExpensive = getCost (trybuy) > moneyamt;
            
            bool expectFail = expectFail_unavailable || expectFail_tooExpensive;
            
            if (buyFailed)
            {
                if (!expectFail)
                {
                    printf ("%s: FAIL when trying to buy an available card %d with sufficient funds %d!\n", __func__, trybuy, moneyamt);
                    return;
                }
                else
                {
                    printf ("%s: PASS when trying to buy ", __func__);
                    if (expectFail_unavailable)
                        printf ("unavailable ");
                    printf ("card %d", trybuy);
                    if (expectFail_tooExpensive)
                        printf (" without sufficient funds %d", moneyamt);
                    printf (".\n");
                }
            }
            else
            {
                if (!expectFail)
                {
                    printf ("%s: PASS when trying to buy an available card %d with sufficient funds %d.\n", __func__, trybuy, moneyamt);
                }
                else
                {
                    printf ("%s: FAIL when trying to buy ", __func__);
                    if (expectFail_unavailable)
                        printf ("unavailable ");
                    printf ("card %d", trybuy);
                    if (expectFail_tooExpensive)
                        printf (" without sufficient funds %d", moneyamt);
                    printf ("!\n");
                    return;
                }
            }
        }
    }
}

int main (int argc, char *argv[])
{
    testBuyCard ();
}
